home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK2.toast / Development Kits (Disc 2) / QuickTime™ VR 2.0 SDK / QTVR C⁄C++ Runtime API / Sample Code / VRShell Sample Code / VRKeypad / Application Files / MacApplication.c < prev    next >
Encoding:
Text File  |  1997-05-22  |  9.8 KB  |  471 lines  |  [TEXT/MPCC]

  1. //
  2. //    File:        MacApplication.c
  3. //
  4. //    Contains:    Application-specific code for keypad navigation demo.
  5. //
  6. //    Written by:    Tim Monroe
  7. //                Based (heavily!) on the MovieShell code written by Apple DTS
  8. //
  9. //    Copyright:    © 1994-1996 by Apple Computer, Inc., all rights reserved.
  10. //
  11. //    Change History (most recent first):
  12. //
  13. //       <5>         12/06/96    rtm        made HandleQTVRKeyPress fully operational
  14. //       <4>         12/05/96    rtm        added hooks into MacFramework.c: StopApplication, InitApplicationWindowObject
  15. //       <3>         12/02/96    rtm        added cursor updating to DoIdle
  16. //       <2>         11/27/96    rtm        conversion to personal coding style;
  17. //                                    added preliminary QTVR support
  18. //       <1>         12/21/94    khs        first file
  19. //       
  20. //
  21.  
  22. // header files
  23. #include "MacApplication.h"
  24. #include "MacFramework.h"
  25. #include "AppConfiguration.h"
  26. #include "DTSQTUtilities.h"
  27. #include "QTVRUtilities.h"
  28.  
  29. // Header file for the specific test functions.
  30. #include "TestFunctions.h"
  31.  
  32. // global variables
  33. long            gMaxMilliSecToUse = kMaxMilliSecToUse;        
  34. Boolean            gQTVRMgrIsPresent = false;    // is the QuickTime VR Manager available?        
  35.  
  36.  
  37. //////////
  38. //
  39. // InitApplication
  40. // Do any application-specific initialization.
  41. //
  42. //////////
  43.  
  44. void InitApplication (void)
  45. {
  46.     // make sure that the QTVR Manager is present in the present operating environment
  47.     if (QTVRUtils_IsQTVRMgrInstalled()) {
  48.         gQTVRMgrIsPresent = true;
  49.     } else {
  50.         ShowWarning("\pThe QuickTime VR Manager cannot be found.", 0);
  51.         //ExitToShell();
  52.     }
  53. }
  54.  
  55.  
  56. //////////
  57. //
  58. // StopApplication
  59. // Do any application-specific shut-down.
  60. //
  61. //////////
  62.  
  63. void StopApplication (void)
  64. {
  65.     // @@@INSERT APPLICATION-SPECIFIC SHUT-DOWN FUNCTIONALITY HERE
  66.  
  67. }
  68.  
  69.  
  70. //////////
  71. //
  72. // DoIdle
  73. // Do any processing that can/should occur at idle time.
  74. //
  75. //////////
  76.  
  77. void DoIdle (WindowRef theWindow)
  78. {
  79.     GrafPtr             mySavedPort;
  80.     WindowObject         myWindowObject;
  81.     MovieController        myMC = NULL;
  82.     Point                myPoint;
  83.     
  84.     GetPort(&mySavedPort);
  85.     SetPort(theWindow);
  86.     
  87.     myWindowObject = (WindowObject) GetWRefCon(theWindow);
  88.     if (myWindowObject != NULL) {
  89.     
  90.         myMC = (**myWindowObject).fController;
  91.         if (myMC != NULL) {
  92.         
  93.             MoviesTask(MCGetMovie(myMC), gMaxMilliSecToUse);
  94.  
  95.             // restore the cursor to the arrow if it's outside the front movie window
  96.             // and the mouse button isn't still down;
  97.             // this is needed only for VR movies, but should probably always be done
  98.             if (theWindow == FrontWindow()) {
  99.                 GetMouse(&myPoint);
  100.                 if (!PtInMovie(MCGetMovie(myMC), myPoint) && !StillDown())
  101.                     InitCursor();
  102.             }
  103.         }
  104.     }
  105.  
  106.     // @@@INSERT ANY OTHER IDLE-BASED FUNCTIONALITY HERE
  107.  
  108.     SetPort(mySavedPort);
  109. }
  110.  
  111.  
  112. //////////
  113. //
  114. // DoUpdateWindow
  115. // Update the specified window.
  116. //
  117. //////////
  118.  
  119. void DoUpdateWindow (WindowRef theWindow, Rect *theRefreshArea)
  120. {
  121.     GrafPtr             mySavedPort;
  122.     
  123.     GetPort(&mySavedPort);
  124.     SetPort(theWindow);
  125.     
  126.     BeginUpdate(theWindow);
  127.  
  128.     EraseRect(theRefreshArea);        // this is important, for non-rectangular movies
  129.     
  130.     // @@@INSERT WINDOW-SPECIFIC DRAWING FUNCTIONALITY HERE
  131.  
  132.     EndUpdate(theWindow);
  133.     SetPort(mySavedPort);
  134. }
  135.  
  136.  
  137. //////////
  138. //
  139. // HandleContentClick
  140. // Handle mouse button clicks in the specified window.
  141. //
  142. //////////
  143.  
  144. void HandleContentClick (WindowRef theWindow, EventRecord *theEvent)
  145. {
  146.     GrafPtr             mySavedPort;
  147.     
  148.     GetPort(&mySavedPort);
  149.     SetPort(theWindow);
  150.  
  151.     // @@@INSERT APPLICATION-SPECIFIC CONTENT CLICKING FUNCTIONALITY HERE
  152.  
  153.     SetPort(mySavedPort);
  154. }
  155.  
  156.  
  157. //////////
  158. //
  159. // HandleQTVRKeyPress
  160. // Handle QuickTime VR-specific key presses.
  161. // Returns true if the key press was handled, false otherwise.
  162. //
  163. //////////
  164.  
  165. Boolean HandleQTVRKeyPress (EventRecord *theEvent)
  166. {
  167.     Boolean            isHandled = true;
  168.     char            myCharCode;
  169.     QTVRInstance    myInstance;
  170.     
  171.     myCharCode = theEvent->message & charCodeMask;
  172.     
  173.     // make sure we can safely call the QTVR API
  174.     if (!gQTVRMgrIsPresent)
  175.         return(false);
  176.     
  177.     myInstance = GetQTVRInstanceFromFrontWindow();
  178.     if (myInstance == NULL)
  179.         return(false);
  180.     
  181.     switch (myCharCode) {
  182.         case '-':
  183.             // decrease pan 1 degree
  184.             ChangePanAngle(myInstance, -1.0);
  185.             break;
  186.         case '+':
  187.             // increase pan 1 degree
  188.             ChangePanAngle(myInstance, 1.0);
  189.             break;
  190.  
  191.         // take keypad keys as a nudge in the appropriate direction
  192.         case '1':
  193.             QTVRNudge(myInstance, kQTVRDownLeft);
  194.             break;
  195.         case '2':
  196.             QTVRNudge(myInstance, kQTVRDown);
  197.             break;
  198.         case '3':
  199.             QTVRNudge(myInstance, kQTVRDownRight);
  200.             break;
  201.         case '4':
  202.             QTVRNudge(myInstance, kQTVRLeft);
  203.             break;
  204.         case '6':
  205.             QTVRNudge(myInstance, kQTVRRight);
  206.             break;
  207.         case '7':
  208.             QTVRNudge(myInstance, kQTVRUpLeft);
  209.             break;
  210.         case '8':
  211.             QTVRNudge(myInstance, kQTVRUp);
  212.             break;
  213.         case '9':
  214.             QTVRNudge(myInstance, kQTVRUpRight);
  215.             break;
  216.  
  217.         default:
  218.             isHandled = false;
  219.             break;
  220.     }
  221.  
  222.     if (isHandled)
  223.         QTVRUpdate(myInstance, kQTVRCurrentMode);
  224.         
  225.     return(isHandled);
  226. }
  227.  
  228.  
  229. //////////
  230. //
  231. // CreateMovieWindow
  232. // Create a window to display a movie in.
  233. //
  234. //////////
  235.  
  236. WindowRef CreateMovieWindow (Rect *theRect, Str255 theTitle)
  237. {
  238.     WindowRef            myWindow;
  239.         
  240.     myWindow = NewCWindow(NULL, theRect, theTitle, false, noGrowDocProc, (WindowPtr) -1L, true, 0);
  241.     return(myWindow);
  242. }
  243.  
  244.  
  245. //////////
  246. //
  247. // HandleApplicationMenu
  248. // Handle selections in the application's menus.
  249. //
  250. //////////
  251.  
  252. void HandleApplicationMenu (short theMenuID, short theMenuItem)
  253. {
  254.     MovieController     myMC = NULL;
  255.     
  256.     switch (theMenuID) {
  257.         case mTesting:
  258.             switch(theMenuItem) {
  259.                 case iTest1:
  260.                     // @@@INSERT YOUR TEST FUNCTION 1 HERE
  261.                     myMC = GetMCFromFrontWindow();
  262.                     if (myMC != NULL) {
  263.                         QTVRUtils_HideControllerBar(myMC);
  264.                     }
  265.                     break;
  266.                 
  267.                 case iTest2:
  268.                     // @@@INSERT YOUR TEST FUNCTION 2 HERE
  269.                     myMC = GetMCFromFrontWindow();
  270.                     if (myMC != NULL) {
  271.                         QTVRUtils_ShowControllerBar(myMC);
  272.                     }
  273.                     break;
  274.                 
  275.                 case iTest3:
  276.                     // @@@INSERT YOUR TEST FUNCTION 3 HERE
  277.                     myMC = GetMCFromFrontWindow();
  278.                     if (myMC != NULL) {
  279.                         QTVRUtils_HideControllerButton(myMC, kQTVRHotSpotButton);
  280.                     }
  281.                     break;
  282.             }
  283.             break;
  284.     }
  285. }
  286.  
  287.  
  288. //////////
  289. //
  290. // AdjustApplicationMenus
  291. // Adjust state of items in the application's menus.
  292. //
  293. //////////
  294.  
  295. void AdjustApplicationMenus (void)
  296. {
  297.     WindowRef             myWindow = NULL;
  298.     MovieController     myMC = NULL;
  299.     
  300.     myMC = GetMCFromFrontWindow();
  301.     if (myMC != NULL) {
  302.         EnableItem(GetMHandle(mTesting), iTest1);
  303.         EnableItem(GetMHandle(mTesting), iTest2);
  304.         EnableItem(GetMHandle(mTesting), iTest3);
  305.     } else {
  306.         DisableItem(GetMHandle(mTesting), iTest1);
  307.         DisableItem(GetMHandle(mTesting), iTest2);
  308.         DisableItem(GetMHandle(mTesting), iTest3);
  309.     }
  310.     
  311.     DisableItem(GetMHandle(mFile), iNew);        // we don't allow creating new VR files here...
  312.     DisableItem(GetMHandle(mFile), iPrint);        // currently printing causes crashes; FIX!
  313. }
  314.  
  315.  
  316. //////////
  317. //
  318. // AddControllerFunctionality
  319. // Configure the movie controller.
  320. //
  321. //////////
  322.  
  323. void AddControllerFunctionality (MovieController theMC)
  324. {
  325.     long            myControllerFlags;
  326.     
  327.     // CLUT table use    
  328.     MCDoAction(theMC, mcActionGetFlags, &myControllerFlags);
  329.     MCDoAction(theMC, mcActionSetFlags, (void *)(myControllerFlags | mcFlagsUseWindowPalette));
  330.  
  331.     // enable keyboard event handling    
  332.     MCDoAction(theMC, mcActionSetKeysEnabled, (void *)true);
  333.     
  334.     // disable drag support
  335.     MCDoAction(theMC, mcActionSetDragEnabled, (void *)false);
  336. }
  337.  
  338.  
  339. //////////
  340. //
  341. // GetQTVRInstanceFromFrontWindow
  342. // Get the QTVRInstance associated with the front window.
  343. //
  344. //////////
  345.  
  346. QTVRInstance GetQTVRInstanceFromFrontWindow (void)
  347. {
  348.     QTVRInstance         myInstance = NULL;
  349.     WindowRef             myWindow = NULL;
  350.     WindowObject        myWindowObject = NULL;
  351.  
  352.     myWindow = FrontWindow();
  353.     if (myWindow == NULL)
  354.         return(NULL);
  355.  
  356.     if (!IsAppWindow(myWindow))
  357.         return(NULL);
  358.             
  359.     myWindowObject = (WindowObject)GetWRefCon(myWindow);
  360.     if (myWindowObject == NULL)
  361.         return(NULL);
  362.         
  363.     MoveHHi((Handle)myWindowObject);
  364.     HLock((Handle)myWindowObject);
  365.  
  366.     // test if this is indeed a movie controller and not an otherwise valid pointer (non-NULL value)
  367.     if (!IsWindowObjectOurs(myWindowObject))
  368.         return(NULL);
  369.         
  370.     myInstance = (**myWindowObject).fInstance;
  371.     HUnlock((Handle)myWindowObject);
  372.     
  373.     return(myInstance);
  374. }
  375.  
  376.  
  377. //////////
  378. //
  379. // InitApplicationWindowObject
  380. // Do any application-specific initialization of the window object.
  381. //
  382. //////////
  383.  
  384. void InitApplicationWindowObject (WindowObject theWindowObject)
  385. {
  386.     Track                myQTVRTrack = NULL;
  387.     OSErr                myErr = noErr;
  388.     Movie                myMovie = NULL;
  389.     MovieController        myMC = NULL;
  390.     QTVRInstance        myInstance = NULL;
  391.         
  392.     if (theWindowObject == NULL)
  393.         return;
  394.  
  395.     // make sure we can safely call the QTVR API
  396.     if (!gQTVRMgrIsPresent)
  397.         return;
  398.  
  399.     // find the QTVR Track
  400.     myMovie = (**theWindowObject).fMovie;
  401.     myMC = (**theWindowObject).fController;
  402.     myQTVRTrack = QTVRGetQTVRTrack(myMovie, 1);
  403.     
  404.     myErr = QTVRGetQTVRInstance(&myInstance, myQTVRTrack, myMC);
  405.     (**theWindowObject).fInstance = myInstance;
  406.  
  407.     // do any QTVR window configuration
  408.     if (myInstance != NULL) {
  409.         // set unit to degrees
  410.         myErr = QTVRSetAngularUnits(myInstance, kQTVRDegrees);
  411.         
  412.     }
  413.     
  414. }
  415.  
  416.  
  417. //////////
  418. //
  419. // RemoveApplicationWindowObject
  420. // Do any application-specific clean-up of the window object.
  421. //
  422. //////////
  423.  
  424. void RemoveApplicationWindowObject (WindowObject theWindowObject)
  425. {
  426.     OSErr                myErr = noErr;
  427.     QTVRInstance        myInstance = NULL;
  428.         
  429.     if (theWindowObject == NULL)
  430.         return;
  431.         
  432.     myInstance = (**theWindowObject).fInstance;
  433.     
  434.     if (myInstance != NULL) {
  435.     
  436.     }
  437.  
  438.     // DoDestroyMovieWindow in MacFramework.c releases the window object itself
  439. }
  440.  
  441.  
  442. //////////
  443. //
  444. // ApplicationMCActionFilterProc 
  445. // Intercept some mc actions for the QuickTime VR movie controller.
  446. //
  447. //////////
  448.  
  449. pascal Boolean ApplicationMCActionFilterProc (MovieController theMC, short theAction, void *theParams, WindowRef theWindow)
  450. {
  451.     Boolean        isHandled = false;
  452.     
  453.     switch (theAction) {
  454.     
  455.         // handle window resizing
  456.         case mcActionControllerSizeChanged: {
  457.             Rect                myMovieBounds;
  458.  
  459.             MCGetControllerBoundsRect(theMC, &myMovieBounds);
  460.             SizeWindow((WindowPtr)theWindow, myMovieBounds.right - myMovieBounds.left,
  461.                                              myMovieBounds.bottom - myMovieBounds.top, true);
  462.             break;
  463.         }
  464.                         
  465.         default:
  466.             break;
  467.     }
  468.     
  469.     return(isHandled);    
  470. }
  471.